﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace Nodejs版本控制器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            UpdateInstallList();
        }
        void UpdateInstallList()
        {
            comboBox1.Items.Clear();
            string[] installVersions = RunCMD("nvm list").Split('\n');
            int selectedIdx = -1;
            for (int i = 1; i < installVersions.Length; i++)
            {
                string version = installVersions[i];
                if (!string.IsNullOrWhiteSpace(version))
                {
                    comboBox1.Items.Add(installVersions[i]);
                } 
                if(version.Contains("*"))
                {
                    selectedIdx = i - 1;
                }
            }
            comboBox1.SelectedIndex = selectedIdx;
            label2.Text = "当前使用版本:" + RunCMD("nvm current");
        }
        private string RunCMD(string cmdStr)
        {
            Process CmdProcess = new Process();
            CmdProcess.StartInfo.FileName = "cmd.exe";
            CmdProcess.StartInfo.CreateNoWindow = true;         // 不创建新窗口    
            CmdProcess.StartInfo.UseShellExecute = false;       //不启用shell启动进程  
            CmdProcess.StartInfo.RedirectStandardInput = true;  // 重定向输入    
            CmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出    
            CmdProcess.StartInfo.RedirectStandardError = true;  
            CmdProcess.StartInfo.Arguments = "/c " + cmdStr;
            CmdProcess.Start();//执行 
            string result= CmdProcess.StandardOutput.ReadToEnd();//获取返回值   
            CmdProcess.WaitForExit();//等待程序执行完退出进程   
            CmdProcess.Close();//结束
            if(result.Contains("不是内部或外部命令"))
            {
                if (DialogResult.OK == MessageBox.Show("你还未安装nvm,你可以点确定到官网下载,或者在菜单里选择安装"))
                {
                    Process.Start("https://github.com/nvm-sh/nvm/releases");          
                }
            }
            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(comboBox1.Text))
            {
                MessageBox.Show("你还未安装Node");
                return;
            }
            RunCMD("nvm use " + comboBox1.Text);
            label2.Text = "当前使用版本:" + RunCMD("nvm current");
            UpdateInstallList();
        }


        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(!((e.KeyChar>='0'&&e.KeyChar<='9')||e.KeyChar=='.'||e.KeyChar=='\b'))
            {
                e.Handled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string result=RunCMD("nvm install " + textBox1.Text);
            if(result.Contains("Version "+ textBox1.Text + " is not available"))
            {
                MessageBox.Show("抱歉没有这个版本");
            }
            else
            {
                MessageBox.Show("Node v" + comboBox1.Text + "安装完成");
            }
            UpdateInstallList();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            RunCMD("nvm uninstall " + comboBox1.Text);
            MessageBox.Show("Node v" + comboBox1.Text + "卸载完成");
            UpdateInstallList();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 安装nvmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Path.Combine(Application.StartupPath, "Tools/nvm-setup.exe"));
        }
    }
}
